package com.pzb.web.controller.wechat;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pzb.common.annotation.Log;
import com.pzb.common.core.controller.BaseController;
import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.enums.BusinessType;
import com.pzb.wechat.domain.WechatApplet;
import com.pzb.wechat.service.IWechatAppletService;
import com.pzb.common.core.page.TableDataInfo;

/**
 * 小程序商户Controller
 * 
 * @author Mr_Wu
 * @date 2022-01-07
 */
@RestController
@RequestMapping("/wechat/applet")
public class WechatAppletController extends BaseController
{
    @Autowired
    private IWechatAppletService wechatAppletService;

    /**
     * 查询小程序商户列表
     */
    @PreAuthorize("@ss.hasPermi('wechat:applet:list')")
    @GetMapping("/list")
    public TableDataInfo list(WechatApplet wechatApplet)
    {
        startPage();
        List<WechatApplet> list = wechatAppletService.selectWechatAppletList(wechatApplet);
        return getDataTable(list);
    }

    /**
     * 查询小程序商户列表
     */
    @GetMapping("/appletList")
    public AjaxResult appletList(WechatApplet wechatApplet)
    {
        List<WechatApplet> list = wechatAppletService.selectWechatAppletList(wechatApplet);
        return AjaxResult.success(list);
    }

    /**
     * 获取小程序商户详细信息
     */
    @PreAuthorize("@ss.hasPermi('wechat:applet:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(wechatAppletService.selectWechatAppletById(id));
    }

    /**
     * 刷新授权账号信息
     */

    @GetMapping(value = "/refreshApplet/{appid}")
    public AjaxResult refreshApplet(@PathVariable("appid") String appid)
    {
        wechatAppletService.refreshApplet(appid);
        return AjaxResult.success();
    }

    /**
     * 新增小程序商户
     */
    @PreAuthorize("@ss.hasPermi('wechat:applet:add')")
    @Log(title = "小程序商户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WechatApplet wechatApplet)
    {
        //查询是否存在该appid
        boolean bl = wechatAppletService.checkExist(wechatApplet.getAppid(),null);
        if(bl){
            return AjaxResult.error("该appid已存在");
        }
        wechatApplet.setCreateUser(getUserId());
        return toAjax(wechatAppletService.insertWechatApplet(wechatApplet));
    }

    /**
     * 修改小程序商户
     */
    @PutMapping
    public AjaxResult edit(@RequestBody WechatApplet wechatApplet)
    {
        //查询是否存在该appid
        boolean bl = wechatAppletService.checkExist(wechatApplet.getAppid(),wechatApplet.getId());
        if(bl){
            return AjaxResult.error("该appid已存在");
        }
        return toAjax(wechatAppletService.updateWechatApplet(wechatApplet));
    }

    /**
     * 删除小程序商户
     */
    @PreAuthorize("@ss.hasPermi('wechat:applet:remove')")
    @Log(title = "小程序商户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids)
    {
        return toAjax(wechatAppletService.deleteWechatAppletById(ids));
    }

    /**
     * 获取权限内的小程序
     */
    @GetMapping("/getAuthorityAppList")
    public AjaxResult getAuthorityAppList()
    {
        List<WechatApplet> list = wechatAppletService.getAuthorityAppList();
        return AjaxResult.success(list);
    }

    /**
     * 获取权限内的小程序
     */
    @GetMapping("/getExtAppid")
    public AjaxResult getExtAppid()
    {
        AjaxResult result = AjaxResult.success();
        result.put("data",wechatAppletService.getExtAppid());
        return result;
    }

    /**
     * 获取权限内的小程序
     */
    @GetMapping("/setExtAppid/{extAppid}")
    public AjaxResult setExtAppid(@PathVariable String extAppid)
    {
        wechatAppletService.setExtAppid(extAppid);
        return AjaxResult.success();
    }
}
